package cn.hfatec.shms.web.xc.xcReport.service;

import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.advice.annotation.OracleDataSource;
import cn.hfatec.shms.web.WeekTime;
import cn.hfatec.shms.web.xc.xcReport.dao.XcReportDao;
import cn.hfatec.shms.web.xc.xcReport.model.XcReport;
import cn.hfatec.shms.web.xc.xcReport.model.XcReportParams;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;


/**
 * @author hfatec
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Service("XcReportService")
public class XcReportService {

    public static final String BEAN_ID = "XcReportService";

    //注入Dao层
    @Resource(name = "XcReportDao")
    private XcReportDao xcReportDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     * 查询数据
     *
     * @param xcReportParams
     * @param page
     */
    @Transactional
    @MysqlDataSource
    public Table queryXcReportTable(XcReportParams xcReportParams, Page page) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", xcReportParams);
        return baseDao.queryTableByPage("queryXcReport", params, page);
    }

    /**
     * @param xcReportParams
     */
    @Transactional
    @MysqlDataSource
    public List<XcReport> queryXcReportXcList(XcReportParams xcReportParams) {

        if (xcReportParams.getWeekNum() == null) {
            xcReportParams.setWeekNum(WeekTime.getWeekNum());
        }

        return xcReportDao.queryXcReport(xcReportParams);

    }

    /**
     * @param xcReportParams
     */
    @Transactional
    @OracleDataSource
    public List<XcReport> queryXcReportXhList(XcReportParams xcReportParams) {

        String a = WeekTime.getFirstDayOfWeek(2017, Integer.valueOf(xcReportParams.getWeekNum()));
        String b = WeekTime.getLastDayOfWeek(2017, Integer.valueOf(xcReportParams.getWeekNum()));
        xcReportParams.setExecuteStart(a);
        xcReportParams.setExecuteEnd(b);
        return xcReportDao.queryXhlist(xcReportParams);

    }

    /**
     * 插入数据
     *
     * @param xcReport
     */
    @Transactional
    public void saveXcReport(XcReport xcReport) {
        if (xcReport.getId() != null) {
            XcReport result = getXcReport(xcReport.getId());
            if (result != null) {
                xcReportDao.update(xcReport);
            } else {
                xcReportDao.insert(xcReport);
            }
        } else {
            xcReportDao.insert(xcReport);
        }
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Transactional
    public void deleteXcReport(Integer id) {

        xcReportDao.delete(id);
    }

    /**
     * 删除数据
     *
     * @param xcReportList
     */
    @Transactional
    public void deleteXcReportBatch(List<String> xcReportList) {
        for (String id : xcReportList) {
            deleteXcReport(Integer.valueOf(id));
        }
    }

    /**
     * 根据ID获取数据
     *
     * @param id
     */
    @Transactional
    public XcReport getXcReport(Integer id) {

        return xcReportDao.getXcReportById(id);
    }
}